<template>
  <el-container class="layout-container">
    <el-header class="header">
      <div class="header-content">
        <h1>图书借阅管理系统</h1>
        <div class="user-info">
          <div class="user-detail">
            <el-icon class="user-icon">
              <User />
            </el-icon>
            <span class="username">欢迎，{{ authStore.user?.username }}</span>
            <el-tag v-if="authStore.isAdmin" type="danger" size="small" class="admin-tag">管理员</el-tag>
          </div>
          <el-button type="primary" plain circle @click="handleLogout" :icon="SwitchButton" class="logout-btn"
            title="注销">
          </el-button>
        </div>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px" class="aside">
        <el-menu :default-active="activeMenu" router class="menu">
          <el-menu-item index="/new-books">
            <el-icon><Star /></el-icon>
            <span>新书推荐</span>
          </el-menu-item>
          <el-menu-item index="/book-borrow">
            <el-icon><Reading /></el-icon>
            <span>图书借阅</span>
          </el-menu-item>
          <el-menu-item index="/current-borrow">
            <el-icon><Clock /></el-icon>
            <span>当前借阅</span>
          </el-menu-item>
          <el-menu-item index="/borrow-records">
            <el-icon><Document /></el-icon>
            <span>借阅记录</span>
          </el-menu-item>
          <el-menu-item index="/favorites">
            <el-icon><StarFilled /></el-icon>
            <span>我的收藏</span>
          </el-menu-item>
          <el-menu-item index="/reservations">
            <el-icon><Calendar /></el-icon>
            <span>我的预约</span>
          </el-menu-item>
          <el-menu-item index="/notifications">
            <el-icon><Bell /></el-icon>
            <span>通知中心</span>
            <el-badge v-if="unreadCount > 0" :value="unreadCount" class="menu-badge" />
          </el-menu-item>
          <el-menu-item index="/profile">
            <el-icon><User /></el-icon>
            <span>个人中心</span>
          </el-menu-item>
          <el-menu-item v-if="authStore.isAdmin" index="/user-management">
            <el-icon><UserFilled /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item v-if="authStore.isAdmin" index="/reports">
            <el-icon><DataAnalysis /></el-icon>
            <span>报表与统计</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  SwitchButton,
  User,
  Star,
  Reading,
  Clock,
  Document,
  StarFilled,
  Calendar,
  Bell,
  UserFilled,
  DataAnalysis
} from '@element-plus/icons-vue'
import { useAuthStore } from '../stores/auth'
import { notificationAPI } from '../services/api'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()
const unreadCount = ref(0)
let timer = null

const activeMenu = computed(() => route.path)

const handleLogout = () => {
  authStore.clearAuth()
  ElMessage.success('已注销')
  router.push('/login')
}

const fetchUnreadCount = async () => {
  try {
    const response = await notificationAPI.getUnreadCount()
    unreadCount.value = response.count || 0
  } catch (error) {
    // 静默失败
  }
}

// 定期刷新未读数量
const startPolling = () => {
  timer = setInterval(() => {
    fetchUnreadCount()
  }, 30000) // 每30秒刷新一次
}

onMounted(() => {
  fetchUnreadCount()
  startPolling()
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  padding: 0;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-content h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 1px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-detail {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.user-icon {
  font-size: 18px;
  color: rgba(255, 255, 255, 0.9);
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: white;
}

.admin-tag {
  margin-left: 4px;
}

.logout-btn {
  border-color: rgba(255, 255, 255, 0.5);
  color: white;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.8);
  transform: scale(1.1);
}

.aside {
  background-color: #304156;
}

.menu {
  border-right: none;
  background-color: #304156;
}

.menu :deep(.el-menu-item) {
  color: #bfcbd9;
}

.menu :deep(.el-menu-item:hover) {
  background-color: #263445;
}

.menu :deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-weight: 500;
}

.menu :deep(.el-menu-item) {
  position: relative;
}

.menu-badge {
  position: absolute;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
}

.main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>
